# -*- coding: utf-8 -*-
import json, requests

from django.http import JsonResponse
from django.views import View
from utils.responses import CommonResponseMixin, ReturnCode

__author__ = 'JK'
__date__ = '2019/4/15 23:02'

# 网易新闻
class NewView(View, CommonResponseMixin):
    def post(self, request):
        received_body = request.body
        received_body = json.loads(received_body)
        page = received_body.get('page')
        count = received_body.get('count')
        url = 'https://api.apiopen.top/getWangYiNews'
        data = {
            "page": page,
            "count": count
        }
        result = requests.post(url=url, data=data).text
        json_result = json.loads(result)
        response = self.wrap_json_response(data=json_result['result'], code=ReturnCode.SUCCESS)
        return JsonResponse(data=response, safe=False)

# 随机古诗词
class PoetryView(View, CommonResponseMixin):
    def post(self, request):
        url = 'https://api.apiopen.top/recommendPoetry'
        json_result = json.loads(requests.post(url=url).text)
        response = self.wrap_json_response(data=json_result['result'], code=ReturnCode.SUCCESS)
        return JsonResponse(data=response, safe=False)